global !p

from builtin import choose_next
from mdtex.scopes import math

special_letters = ["epsilon", "phi"]
letter_prefix = ["", "var"]

endglobal

################################################################
#                                                              #
#               Letters & Unicode - Greek Letter               #
#                                                              #
################################################################

context "math()"
snippet "(?<!\\)\b(alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|omicron|pi|rho|sigma|tau|upsilon|phi|chi|omega|varepsilon|varkappa|vartheta|varpi|varrho|varsigma|varphi)" "希腊字母 Greek Letter" Ar
\\`!p snip.rv = ("var" if match.group(1) in special_letters else "") + match.group(1)`
endsnippet

context "math()"
snippet "(?<!\\)\b(Alpha|Beta|Gamma|Delta|Epsilon|Zeta|Eta|Theta|Iota|Kappa|Lambda|Mu|Nu|Xi|Omicron|Pi|Rho|Sigma|Tau|Upsilon|Phi|Chi|Psi|Omega)" "大写希腊字母 Uppercase Greek Letter" Ar
\\`!p snip.rv = match.group(1)`
endsnippet

context "math()"
snippet "\\(a|b|g|d|z|ep|e|k|i|t|l|m|n|x|u|pi|ps|ph|r|s|o|ve|vp)" "常用希腊字母 Common Greek Letter" r
\\`!p
match match.group(1):
    case "a":
        snip.rv = "alpha"
    case "b":
        snip.rv = "beta"
    case "g":
        snip.rv = "gamma"
    case "d":
        snip.rv = "delta"
    case "z":
        snip.rv = "zeta"
    case "ep":
        snip.rv = "epsilon"
    case "e":
        snip.rv = "eta"
    case "k":
        snip.rv = "kappa"
    case "i":
        snip.rv = "iota"
    case "t":
        snip.rv = "theta"
    case "l":
        snip.rv = "lambda"
    case "m":
        snip.rv = "mu"
    case "n":
        snip.rv = "nu"
    case "x":
        snip.rv = "xi"
    case "u":
        snip.rv = "upsilon"
    case "pi":
        snip.rv = "pi"
    case "ps":
        snip.rv = "psi"
    case "ph":
        snip.rv = "phi"
    case "r":
        snip.rv = "rho"
    case "s":
        snip.rv = "sigma"
    case "o":
        snip.rv = "omega"
    case "ve":
        snip.rv = "varepsilon"
    case "vp":
        snip.rv = "varphi"
`
endsnippet

priority 1
context "math()"
snippet "\\(var)?(epsilon|[tT]heta|[pP]hi|[sS]igma|[kK]appa|[Rr]ho)" "希腊字母斜体 Greek Italic" r
\\`!p snip.rv = choose_next((match.group(1) or ""), letter_prefix, 2) + match.group(2)`
endsnippet

context "math()"
snippet "\\([gG]amma|[dD]elta|[lL]ambda|[Xx]i|[uU]psilon|[pP]si|[oO]mega)" "大写希腊字母" r
\\`!p snip.rv = match.group(1)[0].swapcase() + match.group(1)[1:]`
endsnippet

context "math()"
snippet "\\(var)?([pP]hi)\\" "phi & Phi" r
\\`!p snip.rv = (match.group(1) or "") + match.group(2)[0].swapcase() + match.group(2)[1:]`
endsnippet
